package br.edu.ufcg.ga.parser.args.group;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.ParseException;
import org.jgap.Configuration;
import org.jgap.InvalidConfigurationException;
import org.jgap.impl.SwappingMutationOperator;

import br.edu.ufcg.ga.util.Util;

public class GOswappingMutationOperatorGroupBuilder extends AbstractMyGroupBuilder {

	private Option GOswappingMutationOperator;
	private Option SMOmutationRate;

	@SuppressWarnings("static-access")
	@Override
	protected void init() {
		GOswappingMutationOperator = OptionBuilder
				.withDescription(
						"\"Mutation 1\" do artigo \"Comparative Study of Some Solution Methods for Traveling Salesman Problem Using Genetic Algorithms\"")
				.withLongOpt("GOswappingMutationOperator").create("GO_SMO");

		SMOmutationRate = OptionBuilder.withDescription("Taxa de mutação").withLongOpt("SMOmutationRate")
				.hasArgs(1).withArgName("[0, 1]").create("SMO_MR");

	}

	@Override
	public void configSafely(Configuration config, CommandLine commandLine) throws InvalidConfigurationException,
			ParseException {
		int mutationRate = getMutationRate(commandLine);
		SwappingMutationOperator smo = new SwappingMutationOperator(config, mutationRate);

		config.addGeneticOperator(smo);
	}

	private int getMutationRate(CommandLine commandLine) throws ParseException {
		String mutationRateString = getOptionValue(SMOmutationRate, commandLine);
		double mutationRate = Double.parseDouble(mutationRateString);
		return Util.isEqual(mutationRate, 0.0d) ? 0 : (int) Math.ceil(1 / mutationRate);
	}

	@Override
	protected Option getMainOption() {
		return this.GOswappingMutationOperator;
	}

	@Override
	protected Option[] getOtherOptions() {
		return new Option[] {SMOmutationRate};
	}

}
